require 'View/UIBase/UIBase'

UIActivityCardTips = UIBase.New(PanelResNames.UIOptionalGiftTips)

function UIActivityCardTips.Init(func, selectList, selectNum, chooseItemList)
    UIActivityCardTips.backFunction = func;
    UIActivityCardTips.selectList = selectList;
    UIActivityCardTips.selectNum = selectNum or 0;
    UIActivityCardTips.chooseItemList = chooseItemList;
    UIBase.Init(UIActivityCardTips);
end

function UIActivityCardTips:OnCreate()
    self.candidateList = self:GetChild("candidateList")
    self.inventoryList = self:GetChild("inventoryList")
    self.listShow = self:GetChild("listShow")
    self.progress = self:GetChild("progress")
    self.RCTips = self:GetChild("RCTips")
    self.chooseSureBtn = self:GetChild("chooseSureBtn")
    self:GetChild("tips").text = getLanguage("Storetips1020501");
    self.tipsController = self:GetController("tips")
    self.tipsController.selectedIndex = 0;

    self.inventoryList.itemRenderer = self.listSelectItem;

    self:RegisterOnClick(self:GetChild("chooseSureBtn"),self.OnChooseSure);
    self:RegisterOnClick(self:GetChild("chooseBtn"),self.OnChoose);
    self:RegisterOnClick(self:GetChild("sureBtn"),self.OnSure);
    self:RegisterOnClick(self:GetChild("bgClose"),self.OnClose);
end

function UIActivityCardTips:OnChooseSure()
    if self.playerSelectNum >= self.selectNum then
        self.tipsController.selectedIndex = 1;
        local selectReward = {};
        for i = 1, #self.selectItemIndexs do
            table.insert(selectReward, self.chooseItemList[self.selectItemIndexs[i]]);
            logicMgr.CostManager.RefreshItemList(self.listShow, selectReward);
        end
    else
        UISysTips.AddMsg("请选择您要的奖励");
    end
end

function UIActivityCardTips:OnChoose()
    self.tipsController.selectedIndex = 0;
end

function UIActivityCardTips:OnSure()
    if self.backFunction ~= nil then
        self.backFunction(self.selectItemIndexs);
    end
    self:OnClose();
end

-- 显示
function UIActivityCardTips:OnEnable()
    if self.chooseItemList == nil or (self.selectList == nil and self.selectNum < 1) then
        self:OnClose();
    else
        self:Refresh();
    end
end

function UIActivityCardTips:Refresh()
    self.selectItemData = {};
    self.playerSelectNum = 0;
    self.selectItemIndexs = {};
    self.selectIndex = 0;
    self.itemDialogTips = {};
    if self.tipsController ~= nil then
        self.tipsController.selectedIndex = 0;
    end
    if self.selectList ~= nil then
        self.candidateList:RemoveChildrenToPool();
        self.selectNum = 0;
        for k,v in pairs(self.selectList) do
            self.selectNum = self.selectNum + 1;
            local data = {};
            data.itemData = v;
            local item = self.candidateList:AddItemFromPool();
            item:GetController("choose").selectedIndex = 1;
            data.item = item;
            logicMgr.CostManager.RefreshItem(item:GetChild("item"), v);
            table.insert(self.selectItemData, data);
            self.selectItemIndexs[self.selectNum] = 0;
        end
        self.playerSelectNum = self.selectNum;
    else
        for i = 1, self.selectNum do
            local data = {};
            data.itemData = nil;
            local item = self.candidateList:AddItemFromPool();
            item:GetController("choose").selectedIndex = 0;
            data.item = item;
            table.insert(self.selectItemData, data);
            self.selectItemIndexs[i] = 0;
        end
    end
    self.inventoryList.numItems = #self.chooseItemList;
end

function UIActivityCardTips.listSelectItem(index, itemObj)
    index = index + 1;
    local itemData = UIActivityCardTips.chooseItemList[index];
    local isSelect = UIActivityCardTips:isSelect(itemData[1], index);
    local dialog = itemObj:GetController("dialog");
    local dialogNode = itemObj:GetChild("dialog");
    dialogNode.visible = true;
    UIActivityCardTips.itemDialogTips[index] = dialog;
    if isSelect then
        dialog.selectedIndex = 1;
        UIActivityCardTips.selectIndex = index;
    else
        dialog.selectedIndex = 0;
    end
    logicMgr.CostManager.RefreshItem(itemObj, itemData);
    itemObj.onClick:Clear();
    itemObj.onClick:Add(function()
        -- 临时屏蔽掉，只考虑一种奖励情况
        --if isSelect then
        --    isSelect = false;
        --    dialog.selectedIndex = 0;
        --    if UIActivityCardTips.playerSelectNum > 0 then
        --        UIActivityCardTips:removeItem(itemData[1]);
        --        UIActivityCardTips.playerSelectNum = UIActivityCardTips.playerSelectNum - 1;
        --    end
        --else
        --    if UIActivityCardTips.playerSelectNum >= UIActivityCardTips.selectNum then
        --        UISysTips.AddMsg(getLanguage("ActivityTips2500106"));
        --    else
        --        isSelect = true;
        --        dialog.selectedIndex = 1;
        --        UIActivityCardTips:selectItem(itemData, index);
                --UIActivityCardTips.playerSelectNum = UIActivityCardTips.playerSelectNum + 1;
            --end
        --end
        if UIActivityCardTips.selectIndex == index then
            dialog.selectedIndex = 0;
            UIActivityCardTips.selectIndex = 0;
            if UIActivityCardTips.playerSelectNum > 0 then
                UIActivityCardTips:removeItem(itemData[1]);
                UIActivityCardTips.playerSelectNum = UIActivityCardTips.playerSelectNum - 1;
            end
        else
            if UIActivityCardTips.selectIndex > 0 then
                UIActivityCardTips.itemDialogTips[UIActivityCardTips.selectIndex].selectedIndex = 0;
            end
            dialog.selectedIndex = 1;
            UIActivityCardTips.selectIndex = index;
            UIActivityCardTips:selectItem(itemData, index);
            UIActivityCardTips.playerSelectNum = UIActivityCardTips.playerSelectNum + 1;
        end
    end);
end

function UIActivityCardTips:selectItem(itemData, index)
    --for i = 1, #self.selectItemData do
    --    if self.selectItemData[i].itemData == nil then
    --        local itemObj = self.selectItemData[i].item;
    --        itemObj:GetController("choose").selectedIndex = 1;
    --        logicMgr.CostManager.RefreshItem(itemObj:GetChild("item"), itemData);
    --        self.selectItemData[i].itemData = itemData;
    --        self.selectItemIndexs[i] = index;
        --    break;
        --end
    --end
    local itemObj = self.selectItemData[1].item;
    itemObj:GetController("choose").selectedIndex = 1;
    logicMgr.CostManager.RefreshItem(itemObj:GetChild("item"), itemData);
    self.selectItemData[1].itemData = itemData;
    self.selectItemIndexs[1] = index;
end

function UIActivityCardTips:removeItem(id)
    for i = 1, #self.selectItemData do
        if self.selectItemData[i].itemData ~= nil and self.selectItemData[i].itemData[1] == id then
            local itemObj = self.selectItemData[i].item;
            itemObj:GetController("choose").selectedIndex = 0;
            self.selectItemData[i].itemData = nil;
            self.selectItemIndexs[i] = 0;
            break;
        end
    end
end

function UIActivityCardTips:isSelect(id, index)
    for i = 1, #self.selectItemData do
        if self.selectItemData[i].itemData ~= nil and self.selectItemData[i].itemData[1] == id then
            self.selectItemIndexs[i] = index;
            return true;
        end
    end
    return false;
end

-- 关闭
function UIActivityCardTips:OnClose()
    self:OnBack();
end

-- 隐藏
function UIActivityCardTips:OnDisable()
end

-- 卸载
function UIActivityCardTips:OnDestroy()
end

return UIActivityCardTips